/**
* Add.js controlador para la vista updateinfo.
* Aqui se declaran los eventos utilizados
* en el aplicativo.
*/
Ext.define("entgpv11.controller.Add", {
    extend: "Ext.app.Controller",
    config:
    {
        refs:
        {
            updateinfo: "updateinfo",
            doclist: "doclist"
        },
        control:
        {
            updateinfo:
            {
                saveButtonTapCommand: "onSaveCommand",
                resetButtonTapCommand: "onResetCommand"

            },
            doclist:
            {
                leafitemTapCommand:"onLeafitemCommand"
            }
        }
    },
    // Transitions
    slideLeftTransition: { type: 'slide', direction: 'left' },

    // Commands.
    /**
    * UPDATEINFO:
    * {
    *    saveButtonTapCommand: "onSaveCommand",
    *    resetButtonTapCommand: "onResetCommand"
    * }   
    *        
    */

    /**
    * Graba formulario de avances de un proyecto
    * determinado.
    */
    onSaveCommand: function () {
        try {
            var myform = this.getUpdateinfo();
            var modelData = Ext.create('entgpv11.model.Avance'), errors, errorMessage = ''
            myform.updateRecord(modelData);
            errors = modelData.validate();

            if (errors.isValid()) {
                myform.setMasked({
                    xtype: 'loadmask',
                    message: 'Grabando...'
                });

                 Ext.Ajax.request({
                    url: APP_GLOBAL.getServiceUrl() + 'NewAvance',
                    method: 'POST',
                    disableCaching: false,
                    scriptTag: true,
                    params: myform.getValues(),
                    success: function (response, opts) {
                        myform.setMasked(false);
                        myform.reset();
                        Ext.Msg.alert('Mensaje', 'Los datos se almacenaron exitosamente ', Ext.emptyFn);
                    },
                    failure: function (response, opts) {
                        
                            myform.setMasked(false);
                            Ext.Msg.alert('Error', 'Los datos no pueden ser gravados ', Ext.emptyFn);
                            //console.log('server-side failure with status code ' + response.status);
           
                        
                    },
                    scope: this
                });
            }
            else {
                errors.each(function (err) {
                    errorMessage += err.getMessage() + '<br/>';
                }); // each()
                Ext.Msg.alert('Error', errorMessage);
            }
        } catch (ex) {
            APP_GLOBAL.SetLog('onSaveCommand::' + ex);
        }
    },
    
    /**
    * Limpia formulario
    * avances.
    */
    onResetCommand: function () {
        try {
            var myform = this.getUpdateinfo();
            myform.reset();
        } catch (ex) {
            APP_GLOBAL.SetLog('onResetCommand::' + ex);
        }
    },
     /**
    * DOCLIST:
    * {
    *   leafitemTapCommand:"onLeafitemCommand" 
    *
    * }   
    *        
    */
    /**
    * Evento click seleccionar documento,si el url no es correcto
    * presenta msg <Documento no encontrado>
    * @me
    * @list
    * @index
    * @item
    * @e
    */
    onLeafitemCommand: function (me, list, index, item, e) {
        try {
            var store = list.getStore(),
                        record = store.getAt(index),
                        detailCard = me.getDetailCard();

            list.setMasked({
                xtype: 'loadmask',
                message: 'Loading'
            });

            list.setMasked({
                xtype: 'loadmask',
                message: 'Loading'
            });
            detailCard.setHtml("");
            Ext.Ajax.request({
                url: record.get('path'),
                success: function (response) {
                    detailCard.setViewHtml(response.responseText,record.get('path'));
                    list.unmask();
                },
                failure: function () {
                    detailCard.setHtml("<center>Documento no encontrado</center>");
                    list.unmask();
                }
            });

            list.unmask();



        }
        catch (ex) {
            APP_GLOBAL.SetLog('DocList.addContainerItems::' + ex);
        }

    },
    // Base Class functions.
    launch: function () {
        this.callParent(arguments);
    },
    init: function () {
        this.callParent(arguments);
    }
});